package org.example.lottery_system.common.pojo;

import lombok.Data;
import org.example.lottery_system.common.errorcode.ErrorCode;
import org.example.lottery_system.common.errorcode.GlobalErrorCodeConstants;
import org.springframework.util.Assert;

import java.io.Serializable;
@Data

public class CommonResult<T> implements Serializable {
    /**
     * 返回的状态码
     */
    private Integer code;
    /**
     * 正常返回的数据
     */
    private T data;
    /**
     * 状态码是错误的情况下，错误码的描述
     */
    private String msg;
    public static <T> CommonResult<T> success(T data){
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.data = data;
        commonResult.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        commonResult.msg = "";
        return commonResult;
    }
    public static  <T> CommonResult<T> error(Integer code,String msg){
        System.out.println(code);
        //Assert.isTrue(GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"code不是错误码的异常");
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.msg = msg;
        commonResult.code = code;
        return commonResult;
    }
    public static  <T> CommonResult<T> error(ErrorCode errorCode){
        return error(errorCode.getCode(),errorCode.getMsg());
    }
}
